﻿using SCM.Common;
using SCM.Common.Extensions;
using SCM.Model;
using SCM.Model.DBEntity;
using SCM.Servie.IBusiness;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace SCM.Servie.Business
{
    /// <summary>
    /// 系统成分服务
    /// </summary>
    public class MaterialIngredientService : IMaterialIngredientService
    {
        private readonly DbContext dal = new DbContext();

        public string Add(T_Material_Ingredient model)
        {
            return dal.Insertable(model).ExecuteReturnEntity().CASNo;
        }

        public bool Delete(List<string> casNoList)
        {
            return dal.Updateable<T_Material_Ingredient>(new
            {
                DataStatus = DataStatusEnum.Deleted,
                ModifyTime = DateTime.Now,
            }).Where(p => casNoList.Contains(p.CASNo)).ExecuteCommand() > 0;
        }

        public T_Material_Ingredient GetById(string casNo)
        {
            return dal.Get<T_Material_Ingredient>(casNo);
        }

        public List<T_Material_Ingredient> GetList(string casNo, string cName, string eName)
        {
            Expression<Func<T_Material_Ingredient, bool>> whereExp = (a) => a.DataStatus == DataStatusEnum.Valid;
            if (!string.IsNullOrWhiteSpace(casNo))
            {
                whereExp = whereExp.Compose((a) => a.CASNo.StartsWith(casNo), Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(cName))
            {
                whereExp = whereExp.Compose((a) => a.CName.StartsWith(cName), Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(eName))
            {
                whereExp = whereExp.Compose((a) => a.EName.StartsWith(eName), Expression.AndAlso);
            }
            return dal.Queryable<T_Material_Ingredient>().Where(whereExp).ToList();
        }

        public PageData<T_Material_Ingredient> Query(string casNo, string cName, string eName, PageInfo page)
        {
            int count = 0;
            Expression<Func<T_Material_Ingredient, bool>> whereExp = (a) => a.DataStatus == DataStatusEnum.Valid;
            if (!string.IsNullOrWhiteSpace(casNo))
            {
                whereExp = whereExp.Compose((a) => a.CASNo.StartsWith(casNo), Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(cName))
            {
                whereExp = whereExp.Compose((a) => a.CName.StartsWith(cName), Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(eName))
            {
                whereExp = whereExp.Compose((a) => a.EName.StartsWith(eName), Expression.AndAlso);
            }
            List<T_Material_Ingredient> list = dal.Queryable<T_Material_Ingredient>().Where(whereExp).OrderBy(a => a.CreateTime, SqlSugar.OrderByType.Desc).ToPageList(page.PageIndex, page.PageSize, ref count);
            page.DataCount = count;
            return new PageData<T_Material_Ingredient> { Data = list, Page = page };
        }

        public bool SaveOrUpdate(List<T_Material_Ingredient> list)
        {
            if (list.Count > dal.AddOrSaveDataMax)
            {
                try
                {
                    dal.BeginTran();
                    decimal updateCount = Math.Ceiling(list.Count / (decimal)dal.AddOrSaveDataMax);
                    for (int i = 0; i < updateCount; i++)
                    {
                        int needUpdateCount = (i + 1) * dal.AddOrSaveDataMax > list.Count ? list.Count - i * dal.AddOrSaveDataMax : dal.AddOrSaveDataMax;
                        int result = dal.SaveOrUpdate(list.GetRange(i * dal.AddOrSaveDataMax, needUpdateCount));
                        if (result <= 0)
                        {
                            dal.RollbackTran();
                            return false;
                        }
                    }

                    dal.CommitTran();
                    return true;
                }
                catch (Exception ex)
                {
                    dal.RollbackTran();
                    throw ex;
                }
            }
            else
            {
                return dal.SaveOrUpdate(list) > 0;
            }
        }

        public bool Update(T_Material_Ingredient model)
        {
            return dal.Updateable<T_Material_Ingredient>(p => new T_Material_Ingredient()
            {
                ECNo = model.ECNo,
                CName = model.CName,
                EName = model.EName,
                MF = model.MF,
                ModifyUserId = model.ModifyUserId,
                ModifyUserName = model.ModifyUserName
            }).Where(p => p.CASNo == model.CASNo).ExecuteCommand() > 0;
        }
    }
}
